package demo1;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/24
 * Time: 19:10
 * Description:
 */
class Animal {
    public String name;
    public int age;
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
       //eat();//不要再这里 调用重写的方法
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void eat() {
        System.out.println(this.name+"->正在吃！（Animal）");

    }
}
//理论上的设计 里面应该包含猫 特有的属性或者方法
class Cat extends Animal{
    public Cat(String name,int age) {
        super(name,age);
    }
    @Override//注解：
    public void eat() {
        System.out.println("Cat::eat()");
    }
}

class Bird extends Animal {
    public String swing;//翅膀
    public Bird(String name, int age) {
        super(name, age);
    }

    public void fly() {
        System.out.println(this.name+"->正在飞！");
    }
    @Override
    public void eat() {
        System.out.println("Bird::eat()");
    }
}

public class TestDemo {

    public static void main(String[] args) {
        Animal animal1  = new Cat("一号",12);

    }


    public static void main6(String[] args) {
        Animal animal2  = new Bird("鸟2",99);
        if(animal2 instanceof Bird) {
            Bird bird = (Bird) animal2;
            bird.fly();
        }
        System.out.println("==============");
        Animal animal1  = new Cat("一号",12);
        if(animal1 instanceof Bird) {
            Bird bird2 = (Bird)animal1;
            bird2.fly();
        }else {
            System.out.println("Fdsfsafafdsafa");
        }
    }
    public static void func(Animal animal) {
        animal.eat();
    }

    public static void main5(String[] args) {
        //向上转型->不管猫 还是鸟  都是动物 所以 可以拿Animal接受
        Animal animal1  = new Cat("一号",12);

        Animal animal2  = new Bird("鸟2",99);
        animal2.eat();
        //animal1.fly();缺点

        func(animal1);
        func(animal2);
    }

    public static void main4(String[] args) {
        Cat cat = new Cat("一号",12);
        Bird bird = new Bird("鸟2",99);

        func(cat);
        func(bird);
    }



    public static void main3(String[] args) {
        //向上转型->不管猫 还是鸟  都是动物 所以 可以拿Animal接受
        Animal animal1  = new Cat("一号",12);
        animal1.eat();

        Animal animal2  = new Bird("鸟2",99);
        animal2.eat();
        //animal2.fly(); 父类只能调用自己的成员
    }



    public static Animal func2() {
        int a = 1;
        if(a == 1) {
            return new Cat("一号",12);
        }else {
            return new Bird("鸟2",99);
        }
    }

    public static void main2(String[] args) {
        Cat cat = new Cat("一号",12);
        func(cat);
        //向上转型->不管猫 还是鸟  都是动物 所以 可以拿Animal接受
        Animal animal1  = new Cat("一号",12);

        Animal animal2  = new Bird("鸟2",99);

    }

    public static void main1(String[] args) {
        Cat cat = new Cat("一号",12);
        cat.eat();

        Bird bird = new Bird("鸟2",99);
        bird.eat();
        bird.fly();

        Animal animal = new Animal("animal",88);
        animal.eat();
        //animal.fly(); 一个引用 调用了哪个方法 需要看当前的引用的类型【站在现在这个角度只能看到这里】
    }
}
